from django.test import TestCase

# Create your tests here.
import os


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'orm_check.settings')
    import django
    django.setup()
    import requests
    from app01 import models

    # 增
    # res = models.User.objects.create(name='AK', age=18)
    # print(res)  # 返回创建的对象

    # 删
    # res = models.User.objects.filter(pk=5).delete()  # pk 当前表的主键
    # print(res)  # (1, {'app01.User': 1})

    # res = models.User.objects.filter(pk=1)
    # print(res)  # <QuerySet [<User: User object (1)>]>
    # user_obj = models.User.objects.filter(pk=1).first()
    # print(user_obj)
    # user_obj.delete()

    # 修改
    # res = models.User.objects.filter(name='AKW').update(name='AKW-DSB')
    # print(res)
    # user_obj = models.User.objects.get(pk=4)
    # print(user_obj)  # get直接返回的是当前数据的对象  User object (4),
    # # get数据娶不到不存在会报错，filter不会报错
    # user_obj.name = 'AK'
    # user_obj.save()

    # values 可以指定获取的字段数据 <QuerySet [{'name': 'AK', 'age': 18}]>
    res = models.User.objects.values('name', 'age')
    print(res)
    # values_list <QuerySet [('AK', 18)]>
    res = models.User.objects.values_list('name', 'age')
    print(res)
    print(res.query)  # 查看内部的SQL语句，只能用于QuerySet对象

    # distinct
    res = models.User.objects.all().distinct()
    print(res, res.query)  # 联合去重不要加主键
    # SELECT DISTINCT `app01_user`.`id`, `app01_user`.`name`, `app01_user`.`age`,
    # `app01_user`.`register_time` FROM `app01_user`

    res = models.User.objects.values('name', 'age').distinct()
    print(res, res.query)  #

    # order_by
    res = models.User.objects.order_by('-age')  # 降序
    print(res, res.query)

    # reverse  反转  反转的前提是数据已经排序过了
    res = models.User.objects.all()
    res1 = models.User.objects.reverse()
    res2 = models.User.objects.order_by('age').reverse()
    print(res, res1)
    print(res2)

    # count 统计当前数据的个数
    res = models.User.objects.count()
    print(res)

    res = models.User.objects.exclude(name='AK')
    print(res)

    # 双下划綫
    res = models.User.objects.filter(age__in=[18, 20])
    print(res, res.query)

    res = models.User.objects.filter(name__contains='A')
    print(res, res.query)

    # 忽略大小写, 加i
    res = models.User.objects.filter(name__icontains='a')
    print(res, res.query)

    # 查询注册事件是 2021 1月
    res = models.User.objects.filter(register_time__year='2021')
    print(res, res.query)


def many_check():

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'orm_check.settings')
    import django
    django.setup()
    from app01 import models
    # 一对多外键增删改查
    # # 第一种,直接写实际字段publish_id
    # models.Book.objects.create(title='聊斋', price=444.23, publish_id=2)
    # models.Book.objects.create(title='老子', price=333.23, publish_id=1)

    # 第二种 虚拟字段，放对象
    # publish_obj = models.Publish.objects.filter(pk=2).first()
    # models.Book.objects.create(title='供楼梦', price=666.33, publish=publish_obj)

    # 删除
    # models.Publish.objects.filter(pk=2).delete()  # 级联删除

    # 修改
    # models.Book.objects.filter(pk=1).update(publish_id=2)

    # 多对多  增删改查 就是对第三张表的增删改，因为第三张表不是我们创建的，所以不能通过models使用
    # 添加 add() 参数可以是对象，可放多个
    # book_obj = models.Book.objects.filter(pk=4).first()
    # print(book_obj.authors)  # 就类似于已经到了第三张关系表了
    # # book_obj.authors.add(2)  # 给主键为1的书籍添加一个主键为2 的作者
    # book_obj.authors.add(3, 4)

    # author_obj = models.Author.objects.filter(pk=2).first()
    # author_obj1 = models.Author.objects.filter(pk=3).first()
    # author_obj2 = models.Author.objects.filter(pk=4).first()
    # book_obj.authors.add(author_obj)
    # book_obj.authors.add(author_obj1, author_obj2)

    # 多对多删除 remove() 参数可以是对象，可放多个
    book_obj = models.Book.objects.filter(pk=4).first()
    # book_obj.authors.remove(2)
    # book_obj.authors.remove(3, 4)

    # 修改 set 先删记录然后在添加记录，参数必须是可迭代对象，里面可放对象
    book_obj.authors.set(['3', '2'])

    # 清空 clear
    book_obj.authors.clear()
    return


def kua_table_check():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'orm_check.settings')
    import django
    django.setup()
    from app01 import models
    # 基于对象的跨表查询

    # 查询书籍主键为1的出版社名称
    book_obj = models.Book.objects.filter(pk=1).first()
    publish = book_obj.publish
    print(publish, publish.name)

    # 查询书籍主键为2的作者
    book_obj = models.Book.objects.filter(pk=4).first()
    authors = book_obj.authors
    print(authors)  # app01.Author.None
    print(authors.all())  # <QuerySet [<Author: Author object (2)>, <Author: Author object (3)>]>

    # 3. 查询作者AKW的电话号码
    author_obj = models.Author.objects.filter(name='jason').first()
    phone = author_obj.author_detail.phone
    print(phone)

    """
    在写orm语句时跟写SQL语句一样的
    不要企图一次性将orm语句写完，如果比较复杂，就写一点看一点
    
    正向什么时候需要.all()
        当你的查询结果有多个时需要加.all()
        如果是一个则直接拿到数据对象
    """

    # 查询出版社是东方出版社出版的书, 反向查询，表名小写
    publish_obj = models.Publish.objects.filter(name='东方出版社').first()
    book = publish_obj.book_set.all()  # 有多个表名_set.all()
    print(book)  # <QuerySet [<Book: Book object (4)>, <Book: Book object (6)>]>

    # 查询作者是Jason写过的书
    author_obj = models.Author.objects.filter(name='jason').first()
    book = author_obj.book_set.all()
    print(book)

    # 查询手机号是110的作者姓名
    author_detail_obj = models.AuthorDetail.objects.filter(phone=110).first()
    author = author_detail_obj.author
    print(author.name)


def double_line_kua_table_check():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'orm_check.settings')
    import django
    django.setup()

    from app01 import models
    # 基于双下划綫的跨表查询,相当于mysql里的join
    # 查询Jason的手机号
    phone = models.Author.objects.filter(name='jason').values('author_detail__phone', 'name')
    print(phone)
    phone1 = models.AuthorDetail.objects.filter(author__name='jason').values('author__name', 'phone')
    print(phone1)

    # 查询主键为1的出版社名称和书的名称
    res = models.Publish.objects.filter(book__id=1).values('book__title')
    print(res)
    res = models.Book.objects.filter(pk=1).values('publish__name', 'title', 'publish__book__title')
    print(res)

    # 查询书籍主键为4的作者姓名
    res = models.Book.objects.filter(pk=4).values('authors__name')
    print(res)
    res = models.Author.objects.filter(book__id=4).values('name')
    print(res)

    # 查询书籍主键是1的作者的手机号
    rees = models.Book.objects.filter(pk=1).values('authors__author_detail__phone')
    print(rees)


if __name__ == '__main__':
    # main()
    # many_check()
    # kua_table_check()
    double_line_kua_table_check()